package com.tencent.wxcloudrun.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencent.wxcloudrun.domain.TActiJoinProdAttr;
import com.tencent.wxcloudrun.dto.ActiJoinProdAttrReqDTO;
import com.tencent.wxcloudrun.dto.ActiJoinProdReqDTO;
import com.tencent.wxcloudrun.mapper.TActiJoinProdAttrMapper;
import com.tencent.wxcloudrun.service.TActiJoinProdAttrService;

/**
 * @author ziyin.zhao
 * @description 针对表【t_acti_join_prod_attr(活动参选商品属性表)】的数据库操作Service实现
 * @createDate 2025-04-22 16:43:56
 */
@Service
public class TActiJoinProdAttrServiceImpl extends ServiceImpl<TActiJoinProdAttrMapper, TActiJoinProdAttr>
    implements TActiJoinProdAttrService {

    @Override
    public void joinToDb(Long userId, Long actiId, List<ActiJoinProdReqDTO> prodList) {
        if (CollectionUtils.isEmpty(prodList)) {
            return;
        }
        List<TActiJoinProdAttr> actiJoinProdAttrList = new ArrayList<>(prodList.size());
        for (ActiJoinProdReqDTO actiJoinProdReqDTO : prodList) {
            if (CollectionUtils.isEmpty(actiJoinProdReqDTO.getProdAttrList())) {
                continue;
            }
            for (ActiJoinProdAttrReqDTO actiJoinProdAttrReqDTO : actiJoinProdReqDTO.getProdAttrList()) {
                TActiJoinProdAttr actiJoinProdAttr = new TActiJoinProdAttr();
                actiJoinProdAttr.setActiId(actiId);
                actiJoinProdAttr.setUserId(userId);
                actiJoinProdAttr.setProdId(actiJoinProdReqDTO.getProdId());
                actiJoinProdAttr.setProdAttrId(actiJoinProdAttrReqDTO.getProdAttrId());
                actiJoinProdAttr.setNumber(actiJoinProdAttrReqDTO.getNumber());
                actiJoinProdAttr.setName(actiJoinProdAttrReqDTO.getName());
                actiJoinProdAttr.setValue(actiJoinProdAttrReqDTO.getValue());
                actiJoinProdAttr.setQuantity(actiJoinProdAttrReqDTO.getQuantity());
                actiJoinProdAttr.setCreatedBy(String.valueOf(userId));
                actiJoinProdAttr.setCreatedTime(LocalDateTime.now());
                actiJoinProdAttr.setUpdatedBy(String.valueOf(userId));
                actiJoinProdAttr.setUpdatedTime(LocalDateTime.now());
                actiJoinProdAttrList.add(actiJoinProdAttr);
            }
        }
        this.saveBatch(actiJoinProdAttrList);
    }

    @Override
    public void deleteJoin(Long actiId, Long userId) {
        LambdaQueryWrapper<TActiJoinProdAttr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TActiJoinProdAttr::getActiId, actiId);
        queryWrapper.eq(TActiJoinProdAttr::getUserId, userId);
        queryWrapper.eq(TActiJoinProdAttr::getEnableFlag, 1);
        this.remove(queryWrapper);
    }

    @Override
    public List<Long> actiEnd(Long actiId, Map<Long, List<Integer>> numberMap) {
        List<Long> userIdList = new ArrayList<>();
        List<TActiJoinProdAttr> prodAttrList = new ArrayList<>();
        for (Long actiJoinProdAttrId : numberMap.keySet()) {
            List<Integer> numberList = numberMap.get(actiJoinProdAttrId);
            LambdaQueryWrapper<TActiJoinProdAttr> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TActiJoinProdAttr::getActiId, actiId);
            queryWrapper.in(TActiJoinProdAttr::getNumber, numberList);
            queryWrapper.eq(TActiJoinProdAttr::getEnableFlag, 1);
            List<TActiJoinProdAttr> currList = this.list(queryWrapper);
            if (!CollectionUtils.isEmpty(currList)) {
                prodAttrList.addAll(currList);
            }
        }
        if (!CollectionUtils.isEmpty(prodAttrList)) {
            userIdList = prodAttrList.stream().map(TActiJoinProdAttr::getUserId).collect(Collectors.toList());
        }
        return userIdList;
    }
}
